/**
 * 查看用户
 * 
 */
function viewUser(){
	var rows = $( "#manage-user" ).jqGrid( 'getGridParam', 'selarrrow' );
	var rowsData;
	// 判断是否选中行
	if ( rows.length > 0 ) {
		rowsData = $( "#manage-user" ).jqGrid( "getRowData", rows[0] );
	
	$( "body" ).prepend( '<div class="quanping" ></div>' );
	$( ".quanping" ).after( '<div id="user-view"></div>' );
	$( "#user-view" ).load( "../manage/view/viewUser.html", null, 
		function( response, status, xml ) {
		if ( status === "success" ) {
				$( ".quanping" ).height( $(document).height() );
				$( "#user-view > .manage-frame" ).fadeIn( "normal" );
				var userId=rowsData.id;
				getData( userId );
			} else {
				alert( "查看失败！" );
				$("#user-view, .quanping").remove();
			}
		}
	);
}else{
	alert( "请先选择一行！" );	
	}
}


//删除该用户

function deleteUser() {

	/**
	 * rows 获取所选择行的ID，支持单选、多选
	 *
	 * @type {Object}
	 */

	var rows = $( "#manage-user" ).jqGrid( 'getGridParam', 'selarrrow' );

	/**
	 * rowsData
	 *
	 * @type {Object}
	 */

	var rowsData;

	/**
	 * nodes_userId
	 *
	 * @type {Object}
	 */

	var nodes_userId = new Array();
	
	// 判断是否选中行
	if ( rows.length > 0 ) {
		if ( confirm( "您确认要删除所选的用户吗?" ) ) {

			// 循环遍历所选择行，获取行GlobalID
			for ( var i = 0; i <rows.length; i++ ) {
				rowsData = $( "#manage-user" ).jqGrid( "getRowData", rows[i] );
				nodes_userId.push( rowsData.id );			
			}

			$.ajax({
				type: "POST",
				contentType: "application/x-www-form-urlencoded;charset=UTF-8",
				url: "",
				data: "userIds=" + nodes_userId,
				success: function() {
					alert( "删除成功！" );
					
					// 得到删除前的总记录数和总页数
					var totalRec = parseInt( $("#records").text() );
					var totalPage = parseInt( $("#total").text() );
					
					// 若当前页面只有一条数据且当前总页码数不为1时，删除这条数据后，跳转到第一页
					if ( ( totalRec % 16 === 1 ) && ( totalPage >= 2 ) ) {
						$( "#globalCatalog" ).setGridParam( { page: 1 } ).trigger( "reloadGrid" );
					} else {
						$( "#manage-user" ).trigger( "reloadGrid" );
					}
				},
				error: function(textStatus) {
					alert( textStatus );
				}
			});
		}
	} else {
		alert( "请先选择一行！" );
	}
}

/**
 * getData 根据userId获取详细数据
 * 
 */

function getData(userId) {
	$.ajax({
		type: "POST",
		contentType: "application/x-www-form-urlencoded;charset=UTF-8",
		url: "../base/getUser.html",
		data: "userId=" + userId,
		success: function(result) {
			$("#realName").val(result.realName);
			$("#nickname").val(result.nickname);
			$("#email").val(result.email);
			$("#idNumber").val(result.idNumber);
			var sex = result.sex;
			if(sex=="MALE"){
				sex="男";
			}
			else if(sex=="FEMALE"){
				sex="女";
			}
			else{
				sex="保密";
			}
			
			$("#sex").val(sex);
			$("#role").val("普通用户");
			$("#createtime").val(result.createtimeString);
			$("#income").val(result.income);
			$("#birthday").val(result.birthdayString);
			$("#stringbloodType").val(result.stringbloodType);
			
			
		},
		error: function(textStatus) {
			alert( "加载用户数据失败！" );
		}
	});
}

//冻结该用户

function blockedUser() {

	/**
	 * rows 获取所选择行的ID，支持单选、多选
	 *
	 * @type {Object}
	 */

	var rows = $( "#manage-user" ).jqGrid( 'getGridParam', 'selarrrow' );

	/**
	 * rowsData
	 *
	 * @type {Object}
	 */

	var rowsData;

	
	// 判断是否选中行
	if ( rows.length > 0 ) {
		rowsData = $( "#manage-user" ).jqGrid( "getRowData", rows[0] );
		if(rowsData.stringactivity==="已冻结"){
			alert("此账户已冻结!");
		}else{
		if ( confirm( "您确认要冻结所选的用户吗?" ) ) {
			$.ajax({
				type: "POST",
				contentType: "application/x-www-form-urlencoded;charset=UTF-8",
				url: "../admin/freeze.html",
				data: "userId=" + rowsData.id,
				success: function() {
					alert( "操作成功！" );
					$( "#manage-user" ).trigger( "reloadGrid" );
					
				},
				error: function(textStatus) {
					alert( textStatus );
				}
			});
		}
	} 
	}else {
		alert( "请先选择一行！" );
	}
}

function unblocked() {

	/**
	 * rows 获取所选择行的ID，支持单选、多选
	 *
	 * @type {Object}
	 */

	var rows = $( "#manage-user" ).jqGrid( 'getGridParam', 'selarrrow' );

	/**
	 * rowsData
	 *
	 * @type {Object}
	 */

	var rowsData;

	
	// 判断是否选中行
	if ( rows.length > 0 ) {
		rowsData = $( "#manage-user" ).jqGrid( "getRowData", rows[0] );
		if(rowsData.stringactivity==="正常"){
			alert("此账户是正常的!");
		}else{
		if ( confirm( "您确认要解冻所选的用户吗?" ) ) {
			$.ajax({
				type: "POST",
				contentType: "application/x-www-form-urlencoded;charset=UTF-8",
				url: "../admin/release.html",
				data: "userId=" + rowsData.id,
				success: function() {
					alert( "操作成功！" );
					$( "#manage-user" ).trigger( "reloadGrid" );
					
				},
				error: function(textStatus) {
					alert( textStatus );
				}
			});
		}
	} 
	}else {
		alert( "请先选择一行！" );
	}
}
function upAdmin() {

	/**
	 * rows 获取所选择行的ID，支持单选、多选
	 *
	 * @type {Object}
	 */

	var rows = $( "#manage-user" ).jqGrid( 'getGridParam', 'selarrrow' );

	/**
	 * rowsData
	 *
	 * @type {Object}
	 */

	var rowsData;

	
	// 判断是否选中行
	if ( rows.length == 1 ) {
		rowsData = $( "#manage-user" ).jqGrid( "getRowData", rows[0] );
		if(rowsData.stringactivity!="正常"){
			alert("此账户状态是不正常的!");
		}else{
		if ( confirm( "您确认要提升此用户为管理员吗?" ) ) {
			$.ajax({
				type: "POST",
				contentType: "application/x-www-form-urlencoded;charset=UTF-8",
				url: "../super/authorize.html",
				data: "userId=" + rowsData.id,
				success: function() {
					alert( "操作成功！" );
					$( "#manage-user" ).trigger( "reloadGrid" );
					
				},
				error: function(textStatus) {
					alert( textStatus );
				}
			});
		}
	} 
	}else {
		alert( "请先选择一行！" );
	}
}
